package cn.demographic.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.demographic.entity.RegionAndNumberOfPeople;
import cn.demographic.entity.Registry;
import cn.demographic.entity.User;
import cn.demographic.service.DetailsService;
import cn.demographic.service.FloatingPopulationService;
import cn.demographic.service.WorkingconditionsService;

@RestController
@RequestMapping("/FloatingPopulation")
public class FloatingPopulationController {
	@Autowired
	private WorkingconditionsService workingconditionsService;
	@Autowired
	private FloatingPopulationService floatingPopulationService;
	@Autowired
	private DetailsService detailsService;
	
	/**
	 * 流动人口详情
	 * @param user
	 * @return
	 */
	@RequestMapping("/details")
	@ResponseBody
	public Map<String,Object> finddatails(User user){
		
		return detailsService.DetailsFind(user);
	}
	
	/**
	 * 流动人口信息管理
	 * @return
	 */
	@RequestMapping("/selectUserEmployment")
	@ResponseBody
	public Map<String,Object> selectUserEmployment(String unit,String name,String phone,String stateTime,
			String endTime,String pageNoStr,String pageSizeStr,HttpSession session){
		//获取参数
		long pageNo = 1; //如果没有传数据,默认为第一页
		if( pageNoStr != null && pageNoStr.trim().length()>0 ){
			pageNo = Long.parseLong(pageNoStr);
		}
		int pageSize = 5; //如果没有传数据,默认为10条数据
		if( pageSizeStr != null && pageSizeStr.trim().length()>0 ){
			pageSize = Integer.parseInt(pageSizeStr);
		}
		long beginNo = (pageNo-1)*pageSize;
		//获取地址，市级
		Registry registry = (Registry) session.getAttribute("registry");
		Map<String,Object> mapList = new HashMap<String, Object>();
		mapList.put("liveCity", registry.getLive().getCity() );
		mapList.put("unit", unit);
		mapList.put("name", name);
		mapList.put("phone", phone);
		mapList.put("stateTime", stateTime);
		mapList.put("endTime", endTime);
		mapList.put("beginNo", beginNo);
		mapList.put("pageSize", pageSize);
		List<User> userEmployment = workingconditionsService.selectUserEmployment(mapList);
		Long count = workingconditionsService.countUserEmployment(mapList);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("userEmployment", userEmployment);
		map.put("count", count);
		return map;
	}
	
	/**
	 * 图表展示，
	 * @param createdTime
	 * @return
	 */
	@RequestMapping("/selectYearTime")
	public Map<String,Object> selectYearTime(String Time, String province, String city, String county){
		if( Time == "" ) {
			Date date = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Time = dateFormat.format(date);
		}
		String strtime = Time.substring(0, Time.indexOf("-"));
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("Time", strtime);
		map.put("province", province);
		map.put("city", city);
		map.put("county", county);
		List<RegionAndNumberOfPeople> list = floatingPopulationService.selectRegionPeople(map);
		Map<String,Object> mapList = new HashMap<String,Object>();
		mapList.put("list", list);
		return mapList;
	}
	
	/**
	 * 图表展示，查询图表统计(默认全国省份的人口数量)
	 * @param createdTime
	 * @return
	 */
	@RequestMapping("/selectProvincePeople")
	public Map<String,Object> selectProvincePeople(Map<String,Object> map){
		List<RegionAndNumberOfPeople> list = floatingPopulationService.selectProvincePeople(map);
		Map<String,Object> mapList = new HashMap<String,Object>();
		mapList.put("list", list);
		return mapList;
	}
	
	/**
	 * 本月录入数量
	 * @param createdTime
	 * @return
	 */
	@RequestMapping("/selectCountPeople")
	public Map<String,Object> selectCountPeople(Map<String,Object> map){
		Long count1 = floatingPopulationService.selectCountPeople(map);
		Long count2 = floatingPopulationService.selectCountRegistry(map);
		Long count3 = floatingPopulationService.selectCountNotApplicanttable(map);
		Long count4 = floatingPopulationService.selectCountApplicanttable(map);
		Map<String,Object> mapList = new HashMap<String,Object>();
		mapList.put("count1", count1);
		mapList.put("count2", count2);
		mapList.put("count3", count3);
		mapList.put("count4", count4);
		return mapList;
	}
	
	/**
	 * 查询手机号是否重复
	 * @param mobilePhone
	 * @return
	 */
	@RequestMapping("/selectCountMobilePhone")
	public Map<String ,Object> selectCountMobilePhone(String mobilePhone){
		Map<String ,Object> map = new HashMap<String ,Object>();
		Integer count = floatingPopulationService.selectCountMobilePhone(mobilePhone);
		map.put("count", count);
		return map;
	}
	
	/**
	 * 查询身份证号是否重复
	 * @param citizenshipNumber
	 * @return
	 */
	@RequestMapping("/selectCountCitizenshipNumber")
	public Map<String ,Object> selectCountCitizenshipNumber(String citizenshipNumber){
		Map<String ,Object> map = new HashMap<String ,Object>();
		Integer count = floatingPopulationService.selectCountCitizenshipNumber(citizenshipNumber);
		map.put("count", count);
		return map;
	}
	
}
